package conson.tech.manage.activity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import conson.tech.manage.activity.entity.pojo.UserCreditRating;
import conson.tech.manage.activity.mapper.UserCreditRatingMapper;
import conson.tech.manage.activity.service.UserCreditRatingService;
import conson.tech.manage.api.activity.dto.UserCreditRatingDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * ClassName: UserCreditRatingServiceImpl
 * Description: 用户信用等级
 * Author: HXL
 * Date: 2020/5/27 9:32 上午
 * Version: 1.0
 */
@Slf4j
@Service
@RefreshScope
public class UserCreditRatingServiceImpl implements UserCreditRatingService {

    @Autowired
    private UserCreditRatingMapper userCreditRatingMapper;

    @Value("${userCreditRating.num}")
    private String creditNum;
    @Value("${userCreditRating.desc}")
    private String creditDesc;
    /**
     * Title: 根据手机号查询用户信用
     * Description:
     * @param userMobile
     * @return: conson.tech.manage.api.activity.dto.UserCreditRatingDTO
     * Company: GX
     * Author HXL
     * Date 2020/5/27 11:43 上午
     */
    @Override
    public UserCreditRatingDTO queryUserCredit(String userMobile) {
        UserCreditRatingDTO dto = new UserCreditRatingDTO();
        UserCreditRating rating = new UserCreditRating();
        rating.setDelFlag(1);
        rating.setUserMobile(userMobile);
        rating.setEnableFlag(1);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.setEntity(rating);
        List<UserCreditRating> list = userCreditRatingMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(list)){
            return null;
        }
        BeanUtils.copyProperties(list.get(0), dto);
        return dto;
    }
    /**
     * Title: 根据手机号查询白名单人员
     * Description:
     * @param userMobile
     * @return: conson.tech.manage.api.activity.dto.UserCreditRatingDTO
     * Company: GX
     * Author HXL
     * Date 2020/5/27 11:31 上午
     */
    @Override
    public UserCreditRatingDTO queryUserCreditWhiteList(String userMobile) {
        UserCreditRating rating = new UserCreditRating();
        rating.setDelFlag(1);
        rating.setUserMobile(userMobile);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.setEntity(rating);
        UserCreditRating creditRating = userCreditRatingMapper.selectOne(queryWrapper);
        if (creditRating == null){
            return null;
        }
        UserCreditRatingDTO dto = new UserCreditRatingDTO();
        BeanUtils.copyProperties(creditRating, dto);
        return dto;
    }

    @Override
    @Transactional
    public UserCreditRatingDTO activationUserCredit(String userMobile,Long userId) {
        UserCreditRating rating = new UserCreditRating();
        rating.setCreditNum(Integer.valueOf(creditNum));
        rating.setDelFlag(1);
        rating.setUserId(userId);
        rating.setEnableFlag(1);
        rating.setAssessTime(new Date());
        rating.setCreatedTime(LocalDateTime.now());
        rating.setUpdatedTime(LocalDateTime.now());
        rating.setCreditDesc(creditDesc);
        rating.setUserMobile(userMobile);
        rating.setRating(1);
        rating.setKeepFaithFlag(0);
        userCreditRatingMapper.insert(rating);
        return this.queryUserCredit(userMobile);
    }

    @Override
    @Transactional
    public UserCreditRatingDTO updateUserCredit(String userMobile,Long userId) {
        UserCreditRating rating = new UserCreditRating();
        rating.setEnableFlag(1);
        rating.setUpdatedTime(LocalDateTime.now());
        rating.setUserId(userId);
        ////////设置条件参数/////////////
        UpdateWrapper updateWrapper = new UpdateWrapper();
        UserCreditRating updateRating = new UserCreditRating();
        updateRating.setUserMobile(userMobile);
        updateRating.setDelFlag(1);
        updateWrapper.setEntity(updateRating);
        userCreditRatingMapper.update(rating, updateWrapper);
        return this.queryUserCredit(userMobile);
    }
}
